package com.eyas.business.service.login;

import com.eyas.business.config.webmvc.interceptor.LoginInterceptor;
import com.eyas.business.dao.AdminInfoDao;
import com.eyas.business.model.jpa.AdminInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Auther: 王龙龙
 * @Date: 2019/2/26 14:05
 * @Description:
 */
@Service
@Transactional
public class LoginServiceImpl implements LoginService {
    @Autowired
    private AdminInfoDao adminInfoDao;
    @Override
    public String login(HttpServletRequest request, String username, String password) {
        AdminInfo adminInfo = adminInfoDao.getAdminInfoByLoginnameAndPasswordAndValidflag(username,password,"1");
        if(adminInfo==null) {
            return "{\"success\":false}";
        }else {
            request.getSession().setAttribute(LoginInterceptor.LONGIN_SESSION_NAME,adminInfo);
        }
        return "{\"success\":true}";
    }

    @Override
    public String modifypwd(String pwdold, String pwdnew, HttpSession session) {
        AdminInfo adminInfo = (AdminInfo)session.getAttribute(LoginInterceptor.LONGIN_SESSION_NAME);
        if(pwdold.equalsIgnoreCase(adminInfo.getPassword())) {
            adminInfo.setPassword(pwdnew);
            adminInfoDao.save(adminInfo);
        }else {
            return "{\"success\":false,\"errormsg\":\"旧密码错误\"}";
        }
        return "{\"success\":true}";
    }
}
